/****************************************************************************************************************************************************************
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                      _____  ___________                   ___    ______      ______           _____     _______________________                              *
 *                      __  / / /__  /_  /_____________ _    __ |  / /__(_)________  /_____________  /_    ___    |_____/_/__  __ \                             *
 *                      _  / / /__  /_  __/_  ___/  __ `/    __ | / /__  /_  __ \_  /_  _ \_  __ \  __/    __  /| |___/_/ __  / / /                             *
 *                      / /_/ / _  / / /_ _  /   / /_/ /     __ |/ / _  / / /_/ /  / /  __/  / / / /_      _  ___ |_/_/   _  /_/ /                              *
 *                      \____/  /_/  \__/ /_/    \__,_/      _____/  /_/  \____//_/  \___//_/ /_/\__/      /_/  |_/_/     /_____/                               *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @Project: Ultra Violent - Attack and Defend                                                                                                             *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @Author: Daniel "thiaZ" Koenen                                                                                                                          *
 *          @Used content                                                                                                                                       *
 *              @sscanf Plugin, foreach, Whirlpool Plugin, YSI - Y_Less                                                                                         *
 *              @zcmd - Zeex                                                                                                                                    *
 *              @Streamer Plugin, IRC Plugin - Incognito                                                                                                        *
 *              @dini - Dracoblue                                                                                                                               *
 *              @MySQL Plugin - G-sTyLeZzZ                                                                                                                      *
 *          @Special thanks to                                                                                                                                  *
 *              @Slice                                                                                                                                          *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @File: uvaad.pwn - Main File                                                                                                                            *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @This file is not released under any open source licence, you are not allowed to own this file except you have the author's permission.                 *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @copyright (c) Daniel "thiaZ" Koenen 2010 - 2011 - danielkoenen@t-online.de                                                                             *
 *                                                                                                                                                              *
 ****************************************************************************************************************************************************************/                  
 
Hook:CPlayer_OnPlayerConnect(playerid)
{
    CPlayer::CleanSlot(playerid);

    playerData[playerid][isRegistered] = CAccount::loadPlayer(playerid);
    
    if (playerData[playerid][isRegistered]) {
        if(CAccount::checkAutoLogin(playerid)) {
            SendClientMessage(playerid, C_GREEN, "* You have been automatically logged in, have fun!");
            playerData[playerid][isLogged] = true;
        }
    }
    
    SetPlayerColor(playerid, C_GREY);
    CPlayer::ClearChat(playerid);
    CPlayer::sendConnectMessages(playerid);
    CPlayer::LoadPlayerTextDraws(playerid);
    return true;
}

forward CPlayer::triggerFirstBlood(killerId);
public CPlayer::triggerFirstBlood(killerId)
{
    new szFirstBlood[64];
    
    format(szFirstBlood, 64, "FIRST BLOOD~n~~n~~y~~h~%s", CPlayer::getName(killerId));
    TextDrawSetString(textDraw[firstBlood], szFirstBlood);
    TextDrawShowForAll(textDraw[firstBlood]);
    
    SetTimer("CPlayer_deTriggerFirstBlood", 4000, false);
    return true;
}

forward CPlayer::deTriggerFirstBlood();
public CPlayer::deTriggerFirstBlood()
{
    TextDrawHideForAll(textDraw[firstBlood]);
    return true;
}

stock CPlayer::KickEx(playerId, admin[], reason[], bool:isBan) {
    if(!isBan) {
        SendFormattedMessageToAll(C_YELLOW, "* %s has been kicked by administrator %s. (%s)", nickName(playerId), admin, reason);
        printf("[Kick] %s kicked %s <%s>", admin, nickName(playerId), reason);
        Kick(playerId);
    }
    
    else {
        SendFormattedMessageToAll(C_YELLOW, "* %s has been banned by administrator %s. (%s)", nickName(playerId), admin, reason);
        printf("[Ban] %s kicked %s <%s>", admin, nickName(playerId), reason);
        
        // todo: neu machen
        //playerData[playerId][iBanned] = 1;
        Kick(playerId);
    }
    
    hookIRC("onPlayerKick", "sssi", nickName(playerId), admin, reason, (isBan) ? (1) : (0));
}

stock CPlayer::reloadSkins()
{
    loop_players(playerId) 
    {
        if (playerData[playerId][team] == TEAM_HOME || playerData[playerId][team] == TEAM_HOME_SUB)
            CPlayer::crashSafeSkinSync(playerId, gameConfig[teamSkin][TEAM_HOME]);
        
        if (playerData[playerId][team] == TEAM_AWAY || playerData[playerId][team] == TEAM_AWAY_SUB)
            CPlayer::crashSafeSkinSync(playerId, gameConfig[teamSkin][TEAM_AWAY]);
        
        if (playerData[playerId][team] == TEAM_REF) 
            CPlayer::crashSafeSkinSync(playerId, gameConfig[teamSkin][TEAM_REF]);
    }
    
    return true;
}

stock CPlayer::sendConnectMessages(playerId) 
{
    SendFormattedMessageToAll(C_GREY, "[%d] %s joined the game from %s.", playerId, CPlayer::getName(playerId), GetPlayerCountryName(playerId));
    SendFormattedMessage(playerId, C_PINK, "* Welcome to the server, %s!", CPlayer::getName(playerId));
    SendClientMessage(playerId, C_GREEN, "* Check out /commands and /credits.");
    SendFormattedMessage(playerId, C_GREEN, "* Have fun playing and visit %s.", CLAN_PAGE);
    return true;
}

stock CPlayer::getName(playerId) 
{
    new _sName[MAX_PLAYER_NAME];
    GetPlayerName(playerId, _sName, MAX_PLAYER_NAME);
    return _sName;
}

stock CPlayer::getIP(playerId) 
{
    new _sIP[32];
    GetPlayerIp(playerId, _sIP, 32);
    return _sIP;
}

stock CPlayer::checkRampage(killerId, playerId) 
{
#if FEATURE_RAMPAGE
    playerData[killerId][rampageKills]++;
    
    if(playerData[playerId][rampageKills] >= 4) 
    {
        SendFormattedMessageToAll(C_LIGHTBLUE, "* %s has stopped %s's kill spree. (%d kills in a row)", CPlayer::getName(killerId), CPlayer::getName(playerId), playerData[playerId][rampageKills]);
    }
    
    playerData[playerId][rampageKills] = 0;
    
    new szText[128];
    switch(playerData[killerId][rampageKills]) 
    {
        case 4: format(szText, 128, "~w~- ~b~~h~%s is dominating ~w~-", CPlayer::getName(killerId));
        case 6: format(szText, 128, "~w~- ~b~~h~%s is on a rampage ~w~-", CPlayer::getName(killerId));
        case 8: format(szText, 128, "~w~- ~b~~h~%s is on a killing spree ~w~-", CPlayer::getName(killerId));
        case 10: format(szText, 128, "~w~- ~b~~h~%s has a monsterkill ~w~-", CPlayer::getName(killerId));
        case 12: format(szText, 128, "~w~- ~b~~h~%s is unstoppable ~w~-", CPlayer::getName(killerId));
        case 14: format(szText, 128, "~w~- ~b~~h~%s has a ultrakill ~w~-", CPlayer::getName(killerId));
        case 16: format(szText, 128, "~w~- ~b~~h~%s is godlike ~w~-", CPlayer::getName(killerId));
        case 18: format(szText, 128, "~w~- ~b~~h~%s is wicked sick ~w~-", CPlayer::getName(killerId));
        case 20: format(szText, 128, "~w~- ~b~~h~%s has a ludicrouskill ~w~-", CPlayer::getName(killerId));
        case 22: format(szText, 128, "~w~- ~b~~h~:: %s :: HOLY SHIT :: ~w~-", CPlayer::getName(killerId));
    }
    
    GameTextForAll(szText, 6000, 3);
    rampageSound();
#else
     #pragma unused playerId, killerId
#endif
    return true;
}

stock CPlayer::savePos(playerId, comment[]) 
{
    new Float:_f[4],
        sTemp[128],
        File:fSave = fopen("/Uv_AAD/Positions.txt", io_append);
        
    GetPlayerPos(playerId, _f[0], _f[1], _f[2]);
    GetPlayerFacingAngle(playerId, _f[3]);
    
    format(sTemp, 128, "{%.02f, %.02f, %.02f, %.02f} // %s\r\n", _f[0], _f[1], _f[2], _f[3], comment);
    fwrite(fSave, sTemp);
    fclose(fSave);
    
    SendFormattedMessage(playerId, C_GREEN, "Saved player coordinates! (%s)", sTemp);
    return true;
}

stock CPlayer::savevPos(playerId, comment[]) 
{
    new Float:_f[4],
        iVId = GetPlayerVehicleID(playerId),
        sTemp[128],
        File:fSave = fopen("/Uv_AAD/vPositions.txt", io_append);
        
    new iModel = GetVehicleModel(iVId);
    GetVehiclePos(iVId, _f[0], _f[1], _f[2]);
    GetVehicleZAngle(iVId, _f[3]);
    
    format(sTemp, 128, "CreateVehicle(%d, %.02f, %.02f, %.02f, %.02f, 0, 0, 360); // %s\r\n", iModel, _f[0], _f[1], _f[2], _f[3], comment);
    fwrite(fSave, sTemp);
    fclose(fSave);
    
    SendFormattedMessage(playerId, C_GREEN, "Saved vehicle coordinates! (%s)", sTemp);
    return true;
}

stock CPlayer::setUpForClassSelect(playerId) 
{
    if(playerData[playerId][classCamMoving] == false) 
    {
        playerData[playerId][camPos] = random(sizeof(cameraPos));
        
        new iCamIndex = playerData[playerId][camPos];
        SetPlayerInterior(playerId, cameraPos[iCamIndex][iInteriorX]);
        SetPlayerPos(playerId, cameraPos[iCamIndex][pX], cameraPos[iCamIndex][pY], cameraPos[iCamIndex][pZ]);
        SetPlayerFacingAngle(playerId, cameraPos[0][pA]);
        SetPlayerCameraPos(playerId, cameraPos[iCamIndex][cX], cameraPos[iCamIndex][cY], cameraPos[iCamIndex][cZ]);
        SetPlayerCameraLookAt(playerId, cameraPos[iCamIndex][pX], cameraPos[iCamIndex][pY], cameraPos[iCamIndex][pZ]);
        CPlayer::SixteentoNine(playerId, true);
        
        playerData[playerId][classCamMoving] = true;
        CPlayer::MoveCamera(playerId);
    }
    
    return true;
}

forward alias_MoveCam(playerId);
public alias_MoveCam(playerId) { CPlayer::MoveCamera(playerId); }

stock CPlayer::MoveCamera(playerId) 
{
    if(playerData[playerId][isSpawned] == true) 
    {
        return true;
    }
    
    new iCamIndex = playerData[playerId][camPos],
        Float:X = cameraPos[iCamIndex][cX], 
        Float:Y = cameraPos[iCamIndex][cY], 
        Float:Z = cameraPos[iCamIndex][cZ];
        
    if(playerData[playerId][classDown] == false) 
    {
        playerData[playerId][classCamAngle] += 0.02;
        SetPlayerCameraPos(playerId, X, Y, (Z + playerData[playerId][classCamAngle]));
        SetPlayerCameraLookAt(playerId, cameraPos[iCamIndex][pX], cameraPos[iCamIndex][pY], cameraPos[iCamIndex][pZ]);
        
        if(playerData[playerId][classCamAngle] >= 1.9) 
        {
            playerData[playerId][classDown] = true;
        }
    }
    
    else 
    {
        playerData[playerId][classCamAngle] -= 0.02;
        SetPlayerCameraPos(playerId, X, Y, (Z + playerData[playerId][classCamAngle]));
        SetPlayerCameraLookAt(playerId, cameraPos[iCamIndex][pX], cameraPos[iCamIndex][pY], cameraPos[iCamIndex][pZ]);
        
        if(playerData[playerId][classCamAngle] <= 0.1) 
        {
            playerData[playerId][classDown] = false;
        }
    }
    
    playerData[playerId][classZAngle] += 1.5;
    if(playerData[playerId][classZAngle] >= 360.0) 
    {
        playerData[playerId][classZAngle] = 0.0;
    }
    
    SetPlayerFacingAngle(playerId, playerData[playerId][classZAngle]);
    SetTimerEx("alias_MoveCam", 50, false, "d", playerId);
    return true;
}

stock CPlayer::SyncSkin(playerId) 
{
    if(playerData[playerId][team] == TEAM_HOME || playerData[playerId][team] == TEAM_HOME_SUB) 
    {
        SetPlayerSkin(playerId, gameConfig[teamSkin][TEAM_HOME]);
        return true;
    }
    
    else if(playerData[playerId][team] == TEAM_AWAY || playerData[playerId][team] == TEAM_AWAY_SUB) 
    {
        SetPlayerSkin(playerId, gameConfig[teamSkin][TEAM_AWAY]);
        return true;
    }
 
    else 
    {
        SetPlayerSkin(playerId, gameConfig[teamSkin][TEAM_REF]);
        return true;
    }
}

stock CPlayer::getLevelName(playerId) 
{
    new sOut[24];
    switch(playerData[playerId][level]) 
    {
        case LEVEL_VIP: sOut = "VIP";
        case LEVEL_MOD: sOut = "Moderator";
        case LEVEL_ADMIN: sOut = "Administrator";
        case LEVEL_MANAGEMENT: sOut = "Management";
        default: sOut = "Player";
    }
    
    return sOut;
}

stock CPlayer::UpdatePlayerColor(playerId) 
{
    if(playerData[playerId][activeDM] > -1) 
    {
        SetPlayerColor(playerId, C_GREEN);
        return true;
    }
    
    if(playerData[playerId][team] == TEAM_REF) 
    {
        SetPlayerColor(playerId, C_YELLOW);
        return true;
    }
    
    if(playerData[playerId][isPlaying] && gameData[bIsArena])
    {
        if(playerData[playerId][team] == TEAM_HOME)
        {
            SetPlayerColor(playerId, C_DEF);
        }
        
        if(playerData[playerId][team] == TEAM_AWAY)
        {
            SetPlayerColor(playerId, C_ATT);
        }
        
        return true;
    }
    
    if(playerData[playerId][isPlaying] == true)
    {
        if((playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == true) || (playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == true)) 
        {
            SetPlayerColor(playerId, C_ATT);
            return true;
        }
        
        if((playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == false) || (playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == false)) 
        {
            SetPlayerColor(playerId, C_DEF);
            return true;
        }
    }
    
    else 
    {
        if((playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == true) || (playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == true)) 
        {
            SetPlayerColor(playerId, C_INACTIVE_ATT);
            return true;
        }
        
        if((playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == false) || (playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == false)) 
        {
            SetPlayerColor(playerId, C_INACTIVE_DEF);
            return true;
        }
    }
    
    if((playerData[playerId][team] == TEAM_HOME_SUB && teamData[TEAM_HOME][bAttacking] == true) || (playerData[playerId][team] == TEAM_AWAY_SUB && teamData[TEAM_AWAY][bAttacking] == true)) 
    {
        SetPlayerColor(playerId, C_ATT_SUB);
        return true;
    }
    
    if((playerData[playerId][team] == TEAM_HOME_SUB && teamData[TEAM_HOME][bAttacking] == false) || (playerData[playerId][team] == TEAM_AWAY_SUB && teamData[TEAM_AWAY][bAttacking] == false)) 
    {
        SetPlayerColor(playerId, C_DEF_SUB);
        return true;
    }
    
    return true;
}

stock CPlayer::LoadPlayerTextDraws(playerId) 
{
    /*textDraw[IntroText][playerId] = TextDrawCreate(320.000000, 165.000000, "::  U l t r a   V i o l e n t   A / D  ::");
    TextDrawAlignment(textDraw[IntroText][playerId], 2);
    TextDrawBackgroundColor(textDraw[IntroText][playerId], -736080641);
    TextDrawFont(textDraw[IntroText][playerId], 3);
    TextDrawLetterSize(textDraw[IntroText][playerId], 0.479999, 2.900001);
    TextDrawColor(textDraw[IntroText][playerId], 255);
    TextDrawSetOutline(textDraw[IntroText][playerId], 1);
    TextDrawSetProportional(textDraw[IntroText][playerId], 1);*/
    
/*  textDraw[TeamInfo][playerId] = TextDrawCreate(110.000000, 210.000000, " ");
    TextDrawAlignment(textDraw[TeamInfo][playerId], 2);
    TextDrawBackgroundColor(textDraw[TeamInfo][playerId], 255);
    TextDrawFont(textDraw[TeamInfo][playerId], 1);
    TextDrawLetterSize(textDraw[TeamInfo][playerId], 0.320000, 2.500000);
    TextDrawColor(textDraw[TeamInfo][playerId], -1);
    TextDrawSetOutline(textDraw[TeamInfo][playerId], 1);
    TextDrawSetProportional(textDraw[TeamInfo][playerId], 1);*/
    
    textDraw[PlayerSpectate][playerId] = TextDrawCreate(324.000000, 336.000000, " ");
    TextDrawAlignment(textDraw[PlayerSpectate][playerId], 2);
    TextDrawBackgroundColor(textDraw[PlayerSpectate][playerId], 255);
    TextDrawFont(textDraw[PlayerSpectate][playerId], 1);
    TextDrawLetterSize(textDraw[PlayerSpectate][playerId], 0.309999, 2.099999);
    TextDrawColor(textDraw[PlayerSpectate][playerId], -1);
    TextDrawSetOutline(textDraw[PlayerSpectate][playerId], 0);
    TextDrawSetProportional(textDraw[PlayerSpectate][playerId], 1);
    TextDrawSetShadow(textDraw[PlayerSpectate][playerId], 1);
    
    textDraw[PlayerSpectating][playerId] = TextDrawCreate(498.000000, 328.000000, " ");
    TextDrawBackgroundColor(textDraw[PlayerSpectating][playerId], 255);
    TextDrawFont(textDraw[PlayerSpectating][playerId], 1);
    TextDrawLetterSize(textDraw[PlayerSpectating][playerId], 0.220000, 1.000000);
    TextDrawColor(textDraw[PlayerSpectating][playerId], -1);
    TextDrawSetOutline(textDraw[PlayerSpectating][playerId], 1);
    TextDrawSetProportional(textDraw[PlayerSpectating][playerId], 1);
    
    textDraw[hpLost][playerId] = TextDrawCreate(498.000000, 305.000000, " ");
    TextDrawBackgroundColor(textDraw[hpLost][playerId], 255);
    TextDrawFont(textDraw[hpLost][playerId], 1);
    TextDrawLetterSize(textDraw[hpLost][playerId], 0.389999, 1.200000);
    TextDrawColor(textDraw[hpLost][playerId], -1);
    TextDrawSetOutline(textDraw[hpLost][playerId], 1);
    TextDrawSetProportional(textDraw[hpLost][playerId], 1);
    
    textDraw[teamLeft][playerId] = TextDrawCreate(115.000000, 346.000000, " ");
    TextDrawAlignment(textDraw[teamLeft][playerId], 2);
    TextDrawBackgroundColor(textDraw[teamLeft][playerId], 255);
    TextDrawFont(textDraw[teamLeft][playerId], 1);
    TextDrawLetterSize(textDraw[teamLeft][playerId], 0.430000, 1.800000);
    TextDrawColor(textDraw[teamLeft][playerId], -1);
    TextDrawSetOutline(textDraw[teamLeft][playerId], 1);
    TextDrawSetProportional(textDraw[teamLeft][playerId], 1);

    textDraw[teamMiddle][playerId] = TextDrawCreate(320.000000, 316.000000, " ");
    TextDrawAlignment(textDraw[teamMiddle][playerId], 2);
    TextDrawBackgroundColor(textDraw[teamMiddle][playerId], 255);
    TextDrawFont(textDraw[teamMiddle][playerId], 1);
    TextDrawLetterSize(textDraw[teamMiddle][playerId], 0.490000, 2.699999);
    TextDrawColor(textDraw[teamMiddle][playerId], -1);
    TextDrawSetOutline(textDraw[teamMiddle][playerId], 1);
    TextDrawSetProportional(textDraw[teamMiddle][playerId], 1);

    textDraw[teamRight][playerId] = TextDrawCreate(520.000000, 346.000000, " ");
    TextDrawAlignment(textDraw[teamRight][playerId], 2);
    TextDrawBackgroundColor(textDraw[teamRight][playerId], 255);
    TextDrawFont(textDraw[teamRight][playerId], 1);
    TextDrawLetterSize(textDraw[teamRight][playerId], 0.430000, 1.800000);
    TextDrawColor(textDraw[teamRight][playerId], -1);
    TextDrawSetOutline(textDraw[teamRight][playerId], 1);
    TextDrawSetProportional(textDraw[teamRight][playerId], 1);
    return true;
}

stock CPlayer::DestroyPlayerTextDraws(playerId) 
{
    //TextDrawDestroy(textDraw[IntroText][playerId]);
    //TextDrawDestroy(textDraw[TeamInfo][playerId]);
    TextDrawDestroy(textDraw[PlayerSpectate][playerId]);
    TextDrawDestroy(textDraw[PlayerSpectating][playerId]);
    TextDrawDestroy(textDraw[hpLost][playerId]);
    
    TextDrawDestroy(textDraw[teamLeft][playerId]);
    TextDrawDestroy(textDraw[teamMiddle][playerId]);
    TextDrawDestroy(textDraw[teamRight][playerId]);
    return true;
}

stock CPlayer::ShowTextDraws(playerId) 
{
    TextDrawShowForPlayer(playerId, textDraw[ModeName]);
    TextDrawShowForPlayer(playerId, textDraw[hpLost][playerId]);
    return true;
}

stock CPlayer::ShowLobbyDraws(playerId)
{
    TextDrawShowForPlayer(playerId, textDraw[teamInfo]);
    TextDrawShowForPlayer(playerId, textDraw[teamScores]);
    return true;
}

stock CPlayer::HideLobbyDraws(playerId)
{
    TextDrawHideForPlayer(playerId, textDraw[teamInfo]);
    TextDrawHideForPlayer(playerId, textDraw[teamScores]);
    return true;
}

stock CPlayer::HideTextDraws(playerId) 
{
    TextDrawHideForPlayer(playerId, textDraw[ModeName]);
    TextDrawHideForPlayer(playerId, textDraw[hpLost][playerId]);
    return true;
}

stock CPlayer::SixteentoNine(playerId, bool:toggle) 
{
    if(toggle == true) 
    {
        TextDrawShowForPlayer(playerId, textDraw[StripeUp]);
        TextDrawShowForPlayer(playerId, textDraw[StripeDown]);
    }
    
    else if(toggle == false) 
    {
        TextDrawHideForPlayer(playerId, textDraw[StripeUp]);
        TextDrawHideForPlayer(playerId, textDraw[StripeDown]);
    }
    
    return true;
}

stock CPlayer::ShowWeaponMenu(playerId) 
{
    new sWeaponSet[MAX_WEAPONSETS][128],
        sDialogText[2048];
    for(new i; i < MAX_WEAPONSETS; i++) 
    {
        if(strlen(getWeaponNameEx(weaponSets[i][0])) >= 4 && i < 10) 
        {
            format(sWeaponSet[i], 128, "#%d\t{FF0000}Primary: %s\t\t{0AE2FA}Secondary: %s", i, getWeaponNameEx(weaponSets[i][0]), getWeaponNameEx(weaponSets[i][1]));
        }
            
        else 
        {
            format(sWeaponSet[i], 128, "#%d\t{FF0000}Primary: %s\t\t\t{0AE2FA}Secondary: %s", i, getWeaponNameEx(weaponSets[i][0]), getWeaponNameEx(weaponSets[i][1]));
        }
    }
        
    for(new i; i < MAX_WEAPONSETS; i++) 
    {
        format(sDialogText, 2048, "%s%s\n", sDialogText, sWeaponSet[i]);
    }   

    ShowPlayerDialog(playerId, DIALOG_WEAPONS, DIALOG_STYLE_LIST, "Choose your weapon set", sDialogText, "Accept", "");
    return true;
}

stock CPlayer::showTeamInfo(playerId, classId) 
{
    new szTeamInfo[128];
    switch(classId) 
    {
        case 0:
        {
            format(szTeamInfo, 128, "~p~~h~Auto Assign");
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
            
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
            
            format(szTeamInfo, 128, "%s~h~%s", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
        
        case 1: 
        {
            format(szTeamInfo, 128, "%s~h~%s", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "~p~~h~Auto Assign");
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
        
        case 2: 
        {
            format(szTeamInfo, 128, "%s~h~%s", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "~y~~h~Referee");
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
        
        case 3: 
        {
            format(szTeamInfo, 128, "~y~~h~Referee");
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
        
        case 4: 
        {
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "~y~~h~Referee");
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
        
        case 5: 
        {
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (!homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[awayName]);
            TextDrawSetString(textDraw[teamMiddle][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "%s~h~%s (Sub)", (homeAttacking()) ? (ATT_TAG) : (DEF_TAG), gameConfig[homeName]);
            TextDrawSetString(textDraw[teamLeft][playerId], szTeamInfo);
                
            format(szTeamInfo, 128, "~p~~h~Auto Assign");
            TextDrawSetString(textDraw[teamRight][playerId], szTeamInfo);
        }
    }
    
    TextDrawShowForPlayer(playerId, textDraw[teamLeft][playerId]);
    TextDrawShowForPlayer(playerId, textDraw[teamMiddle][playerId]);
    TextDrawShowForPlayer(playerId, textDraw[teamRight][playerId]);
    return true;
}

stock CPlayer::ClearChat(playerId) 
{
    for(new i; i < 50; i++) 
    {
        SendClientMessage(playerId, C_WHITE, " ");
    }
    
    return true;
}

stock CPlayer::SetUpPingText(playerId) 
{
    if(playerData[playerId][pingLabelId] != Text3D:INVALID_3DTEXT_ID) 
    {
        CPlayer::Destroy3DPingText(playerId);
    }
    
    new iVWorld = GetPlayerVirtualWorld(playerId);
    playerData[playerId][pingLabelWorld] = iVWorld;
    
    playerData[playerId][pingLabelId] = Create3DTextLabel("Initializing...", 0xFFFFFF55, 0.0, 0.0, 0.0, 15.0, playerData[playerId][pingLabelWorld], true);  
    Attach3DTextLabelToPlayer(playerData[playerId][pingLabelId], playerId, 0.0, 0.0, -0.3);
    CPlayer::UpdatePingText(playerId);
    return true;
}

forward alias_UpdatePingText(playerId);
public alias_UpdatePingText(playerId) { return CPlayer::UpdatePingText(playerId); }

stock CPlayer::UpdatePingText(playerId) 
{
    if(playerData[playerId][pingLabelWorld] != GetPlayerVirtualWorld(playerId)) 
    {
        CPlayer::SetUpPingText(playerId);
        return true;
    }
    
    if(playerData[playerId][isSpawned] == true && IsPlayerConnected(playerId)) 
    {
        new sTempFormat[128],
            iPing = GetPlayerPing(playerId),
            _iFPS = playerData[playerId][fps];
            
        
        format(sTempFormat, 128, "{FFFFFF}Ping: {56ABE3}%d\n{FFFFFF}FPS: {56ABE3}%d", iPing, _iFPS);
        
        Update3DTextLabelText(playerData[playerId][pingLabelId], 0xFFFFFFFF, sTempFormat);
        playerData[playerId][pingLabelTimer] = SetTimerEx("alias_UpdatePingText", 500, 0, "d", playerId);
        return true;
    }
    
    else 
    {
        CPlayer::Destroy3DPingText(playerId);
        return true;
    }
}

stock CPlayer::isAttacking(playerId) 
{
    if(playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == true || playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == true) 
    {
        return true;
    }
    
    else if(playerData[playerId][team] == TEAM_HOME && teamData[TEAM_HOME][bAttacking] == false || playerData[playerId][team] == TEAM_AWAY && teamData[TEAM_AWAY][bAttacking] == false) 
    {
        return false;
    }
    
    else
    { 
        return -1;
    }
}

stock CPlayer::Destroy3DPingText(playerId) 
{
    Delete3DTextLabel(playerData[playerId][pingLabelId]);
    playerData[playerId][pingLabelId] = Text3D:INVALID_3DTEXT_ID;
    KillTimer(playerData[playerId][pingLabelTimer]);
    return true;
}

stock CPlayer::CleanSlot(playerId) 
{
    //Integers
    playerData[playerId][kills] = 0;
    playerData[playerId][deaths] = 0;
    playerData[playerId][level] = 0; 
    playerData[playerId][camPos] = -1;
    playerData[playerId][rampageKills] = 0;
    playerData[playerId][team] = NO_TEAM;
    playerData[playerId][duelsWon] = 0;
    playerData[playerId][activeDM] = -1;
    playerData[playerId][mvp] = 0;
    playerData[playerId][basesWon] = 0;
    playerData[playerId][basesLost] = 0;
    playerData[playerId][weaponSelected] = -1; 
    playerData[playerId][restrictionSelected] = -1; 
    playerData[playerId][pingLabelWorld] = 0;
    playerData[playerId][pingLabelTimer] = -1;
    playerData[playerId][spectateId] = INVALID_PLAYER_ID;
    playerData[playerId][spawnedVehicles] = 0; 
    playerData[playerId][baseVehicleId] = INVALID_VEHICLE_ID;
    playerData[playerId][currentVehicle] = INVALID_VEHICLE_ID;
    playerData[playerId][wrongPW] = 0;
    playerData[playerId][healthObject] = INVALID_OBJECT_ID;
    playerData[playerId][armourObject] = INVALID_OBJECT_ID;
    playerData[playerId][lastUpdated] = 0;
    playerData[playerId][lastDrunk] = 0;
    playerData[playerId][fps] = 0;
    playerData[playerId][lastDamage] = 0;
    playerData[playerId][hitsReceived] = 0;
    
    statsInfo[playerId][iSessionKills] = 0;
    statsInfo[playerId][iSessionDeaths] = 0;
    statsInfo[playerId][iRoundKills] = 0;
    statsInfo[playerId][iRoundDeaths] = 0;
    statsInfo[playerId][iCWKills] = 0;
    statsInfo[playerId][iCWDeaths] = 0;
    
    //Booleans
    playerData[playerId][isRegistered] = false;
    playerData[playerId][isLogged] = false;
    playerData[playerId][isDuel] = false;
    playerData[playerId][isDMSpawn] = false;
    playerData[playerId][isSpawned] = false;
    playerData[playerId][isPlaying] = false;
    playerData[playerId][isSpectatingDuel] = false;
    playerData[playerId][isPaused] = false;
    playerData[playerId][isSpectating] = false;
    playerData[playerId][isAFK] = false;
    playerData[playerId][isSyncSpawn] = false;
    playerData[playerId][isAutoAssigned] = false;
    playerData[playerId][classDown] = false;
    playerData[playerId][classCamMoving] = false;
    
    //Floats
    playerData[playerId][recentDamage] = 0.0;
    playerData[playerId][syncX] = 0.0;
    playerData[playerId][syncY] = 0.0;
    playerData[playerId][syncZ] = 0.0;
    playerData[playerId][syncA] = 0.0;
    playerData[playerId][syncHealth] = 0.0;
    playerData[playerId][syncArmour] = 0.0;
    playerData[playerId][classCamAngle] = 0.0;
    playerData[playerId][classZAngle] = 0.0;
    
    //Others
    Delete3DTextLabel(playerData[playerId][pingLabelId]);
    playerData[playerId][pingLabelId] = Text3D: INVALID_3DTEXT_ID;
    return true;
}

stock CPlayer::setPlayerHP(playerId, Float:fHealth)
{
    if(fHealth > 100.0)
    {
        SetPlayerHealth(playerId, 100.0);
        SetPlayerArmour(playerId, (fHealth - 100.0));
    }
    
    else
    {
        SetPlayerHealth(playerId, fHealth);
        SetPlayerArmour(playerId, 0.0);
    }
    
    return true;
}

stock Float:CPlayer::getPlayerHP(playerId)
{
    new Float:  _fH,
        Float:  _fA;
                
    GetPlayerHealth(playerId, _fH);
    GetPlayerArmour(playerId, _fA);
    return floatadd(_fH, _fA);
}

forward alias_Spectate(playerId, spectatee, bool:bFirst);
public alias_Spectate(playerId, spectatee, bool:bFirst) 
{
    CPlayer::Spectate(playerId, spectatee, bFirst);
    CPlayer::UpdateSpecDraw(spectatee);
    return true;
}

stock CPlayer::Spectate(playerId, spectatee, bool:bFirst) 
{
    new szFormatTextDraw[256];
    new Float:fpH,
        Float:fpA;
    
    if(bFirst == true) 
    {
        SetPlayerInterior(playerId, GetPlayerInterior(spectatee));
        SetPlayerVirtualWorld(playerId, GetPlayerVirtualWorld(spectatee));
        
        playerData[playerId][isSpectating] = true;
        playerData[playerId][spectateId] = spectatee;
        TextDrawSetString(textDraw[PlayerSpectate][playerId], "~r~Initializing...");
        TextDrawShowForPlayer(playerId, textDraw[PlayerSpectate][playerId]);
        TogglePlayerSpectating(playerId, true);
        
        if(!IsPlayerInAnyVehicle(spectatee))
        {
            PlayerSpectatePlayer(playerId, spectatee);
        }
        
        else
        {
            new _vId = GetPlayerVehicleID(spectatee);
            PlayerSpectateVehicle(playerId, _vId);
        }
        
        CPlayer::Spectate(playerId, spectatee, false);
        return true;
    }
    
    GetPlayerHealth(spectatee, fpH);
    GetPlayerArmour(spectatee, fpA);

    format(szFormatTextDraw, 256, "~r~Spectating...~n~~w~%s ~r~/~w~ HP: %d ~r~/~w~ Ping: %d ~r~/~w~ FPS: %d ~r~/~w~ Weapon: %s", CPlayer::getName(spectatee), 
                                                                                                                                  floatround((fpH + fpA)),
                                                                                                                                  CPlayer::GetTextDrawPing(spectatee),
                                                                                                                                  playerData[spectatee][fps],
                                                                                                                                  getWeaponNameEx(GetPlayerWeapon(spectatee)));
    
    TextDrawSetString(textDraw[PlayerSpectate][playerId], szFormatTextDraw);
    
    if(playerData[spectatee][isSpawned] == false) 
    {
        TogglePlayerSpectating(playerId, false);
        CPlayer::SpawnEx(playerId);
        return true;
    }
    
    else if(playerData[playerId][isSpectating] == true) 
    {
        SetTimerEx("alias_Spectate", 1500, false, "ddb", playerId, spectatee, false);
        return true;
    }
    
    else 
    {
        TogglePlayerSpectating(playerId, false);
        return true;
    }
}

forward alias_Unspectate(playerId);
public  alias_Unspectate(playerId) 
{
    CPlayer::Unspectate(playerId);
    return true;
}

stock CPlayer::Unspectate(playerId) 
{
    if(playerData[playerId][isSpectating] == true)
    {
        playerData[playerId][isSpectating] = false;
        TextDrawHideForPlayer(playerId, textDraw[PlayerSpectate][playerId]);
        TogglePlayerSpectating(playerId, false);
        CPlayer::SpawnEx(playerId);
        CPlayer::UpdateSpecDraw(playerData[playerId][spectateId]);
        playerData[playerId][spectateId] = INVALID_PLAYER_ID;
    }
}
    
stock CPlayer::GetTextDrawPing(playerId) 
{
    new szFormat[128],
        iPing;
        
    iPing = GetPlayerPing(playerId);
    
    if(iPing >= 200) 
    {
        format(szFormat, 128, "~r~%d", iPing);
    }
    
    else if(iPing < 200 && iPing >= 100) 
    {
        format(szFormat, 128, "~y~%d", iPing);
    }
    
    else if(iPing > 0 && iPing < 100) 
    {
        format(szFormat, 128, "~g~%d", iPing);
    }
    
    return szFormat;
}

forward alias_Sync(playerId);
public alias_Sync(playerId) 
{
    CPlayer::Sync(playerId);
    return true;
}

stock CPlayer::crashSafeSkinSync(playerId, skinId = -1)
{
    if(skinId == -1)
    {
        new nPlayerTeam = playerData[playerId][team],
            nSkin;
    
        switch(nPlayerTeam)
        {
            case TEAM_HOME, TEAM_HOME_SUB: nSkin = gameConfig[teamSkin][TEAM_HOME];
            case TEAM_AWAY, TEAM_AWAY_SUB: nSkin = gameConfig[teamSkin][TEAM_AWAY];
            case TEAM_REF: nSkin = gameConfig[teamSkin][TEAM_REF];
        }

        SetSpawnInfo(playerId, NO_TEAM, nSkin, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0); 
        return true;
    }
    
    SetSpawnInfo(playerId, NO_TEAM, skinId, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0); 
    CPlayer::Sync(playerId);
    return true;
}

stock CPlayer::Sync(playerId) 
{
    playerData[playerId][isSyncSpawn] = true;
    
    GetPlayerPos(playerId, playerData[playerId][syncX], playerData[playerId][syncY], playerData[playerId][syncZ]);
    GetPlayerFacingAngle(playerId, playerData[playerId][syncA]);
    GetPlayerHealth(playerId, playerData[playerId][syncHealth]);
    GetPlayerArmour(playerId, playerData[playerId][syncArmour]);
    
    for(new i; i < 12; i++) 
    {
        GetPlayerWeaponData(playerId, i, playerData[playerId][syncWeapons][i], playerData[playerId][syncAmmo][i]);
    }
    
    playerData[playerId][syncInterior] = GetPlayerInterior(playerId);
    playerData[playerId][syncWorld] = GetPlayerVirtualWorld(playerId);
    CPlayer::SpawnEx(playerId);
}

stock CPlayer::ResetMapIcon(playerId) 
{
    loop_players(i) 
    {
        SetPlayerMarkerForPlayer(i, playerId, GetPlayerColor(playerId));
    }
    return true;
}

stock CPlayer::SpawnEx(playerId) 
{
    if(IsPlayerInAnyVehicle(playerId)) 
    {
        new Float:X,
            Float:Y,
            Float:Z;
        
        GetPlayerPos(playerId, X, Y, Z);
        SetPlayerPos(playerId, X, Y, (Z + 1.0));
    }
    
    SpawnPlayer(playerId);
    return true;
}

stock CPlayer::UpdateSpecDraw(playerId) 
{
    new szTextDrawTemp[256],
        iX;
        
    format(szTextDrawTemp, 256, "~l~- ~y~Spectating you ~l~-~n~~w~");
    loop_players(i) 
    {
        if(playerData[i][spectateId] == playerId) 
        {
            format(szTextDrawTemp, 256, "%s%s~n~", szTextDrawTemp, CPlayer::getName(i));
            iX++;
        }
    }
    
    if(!iX) 
    {
        TextDrawHideForPlayer(playerId, textDraw[PlayerSpectating][playerId]);
        TextDrawSetString(textDraw[PlayerSpectating][playerId], "Initializing...");
    }
    
    else 
    {
        TextDrawSetString(textDraw[PlayerSpectating][playerId], szTextDrawTemp);
        TextDrawShowForPlayer(playerId, textDraw[PlayerSpectating][playerId]);
    }
    
    return true;
}


